﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Sales;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using DevExpress.XtraEditors;

namespace ESOL.WinUI.Sales
{
    public partial class frmMaintainDeliveryItemsQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region vars
        private List<SalesDeliveryItemEntity> mSelectedItems;
        public List<SalesDeliveryItemEntity> SelectedItems
        {
            get { return mSelectedItems; }
        }
        private bool mReturnDataTable = false;
        private DataTable mSelectedDataTable;
        public DataTable SelectedDataTable
        {
            get { return mSelectedDataTable; }
        }
        public string BillingStatus
        {
            get { return cmbBillingStatus.Text; }
            set { cmbBillingStatus.Text = value; }
        }
        public string PayStatus
        {
            get { return cmbReceivedStatus.Text; }
            set { cmbReceivedStatus.Text = value; }
        }
        #endregion

        #region ctos
        public frmMaintainDeliveryItemsQuery(bool returnDataTable)
        {
            InitializeComponent();
            mReturnDataTable = returnDataTable;
        }
        #endregion

        #region functions
        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(cmbBillingStatus.Text) == false)
            {
                if (cmbBillingStatus.Text == "已开票")
                {
                    condition += " AND X.DELIVERY_ITEM_ID IS NOT NULL";
                }
                else
                {
                    condition += " AND X.DELIVERY_ITEM_ID IS NULL";
                }
            }
            if (string.IsNullOrEmpty(cmbReceivedStatus.Text) == false)
            {
                if (cmbReceivedStatus.Text == "已收款")
                {
                    condition += " AND Y.DELIVERY_ITEM_ID IS NOT NULL";
                }
                else
                {
                    condition += " AND Y.DELIVERY_ITEM_ID IS NULL";
                }
            }
            if (dtpDeliveryDate.EditValue!=null)
            {
                condition += " AND SDO.DELIVERY_DATE >= CONVERT(DATETIME,'" + dtpDeliveryDate.EditValue.ToString() + "')";
            }
            if (dtpDeliveryDateTo.EditValue != null)
            {
                condition += " AND SDO.DELIVERY_DATE <= CONVERT(DATETIME,'" + dtpDeliveryDateTo.EditValue.ToString() + "')";
            }
            if (string.IsNullOrEmpty(txtProductCode.Text) == false)
            {
                condition += " AND P.PRODUCT_CODE LIKE '%" + txtProductCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtDeliveryOrderCode.Text) == false)
            {
                condition += " AND SDO.DELIVERY_ORDER_CODE LIKE '%" + txtDeliveryOrderCode.Text + "%' ";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "SDO.DELIVERY_ORDER_CODE DESC";
            DataTable dt = SalesDeliveryItemBs.GetByCondition(condition, orderBy);
            DataColumn dc = new DataColumn("SelectedFlag", typeof(bool));
            dc.DefaultValue = false;
            dt.Columns.Add(dc);
            gvMain.DataSource = dt;
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindDataGrid();
        }
        protected override void Click_Query()
        {
            BindDataGrid();
        }
        protected override void Click_Confirm()
        {
            if (mReturnDataTable)
            {
                mSelectedDataTable = ((DataTable)gvMain.DataSource).Clone();
                DataTable dt= gvMain.DataSource as DataTable;
                foreach(DataRow dr in dt.Rows)
                {
                    if ((bool)dr["SelectedFlag"])
                    {
                        DataRow row = mSelectedDataTable.NewRow();
                        foreach (DataColumn column in mSelectedDataTable.Columns)
                        {
                            row[column.ColumnName] = dr[column.ColumnName];
                        }
                        mSelectedDataTable.Rows.Add(row);
                    }
                }
                if (mSelectedDataTable == null || mSelectedDataTable.Rows.Count == 0)
                {
                    MessageBox.Show("请选择一个入库单明细！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                mSelectedItems = new List<SalesDeliveryItemEntity>();
                DataTable dt = gvMain.DataSource as DataTable;
                foreach (DataRow dr in dt.Rows)
                {
                    if ((bool)dr["SelectedFlag"])
                    {
                        SalesDeliveryItemEntity entity = new SalesDeliveryItemEntity(dr);
                        mSelectedItems.Add(entity);
                    }
                }
                if (mSelectedItems == null || mSelectedItems.Count == 0)
                {
                    MessageBox.Show("请选择一个入库单明细！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            this.DialogResult = DialogResult.OK;
        }

        DataRow drSelected;
        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            drSelected = gridView1.GetDataRow(e.FocusedRowHandle);
        }

        private void repositoryItemCheckEdit1_Click(object sender, EventArgs e)
        {
            if ((bool)drSelected["SelectedFlag"])
            {
                drSelected["SelectedFlag"] = false;
            }
            else
            {
                drSelected["SelectedFlag"] = true;                
            }
            ((CheckEdit)sender).Refresh();
        }

        #endregion
    }
}
